package com.guli.sysuser.controller;

import com.guli.common.vo.R;
import com.guli.sysuser.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoHua666
 * @date 2019/4/19
 */
@RestController
@CrossOrigin
@Api(description = "系统用户管理")
@RequestMapping("/admin/sysuser")
public class SysUserController {

    @ApiOperation(value = "用户登录")
    @PostMapping(value = "login")
    public R login(
            @ApiParam(value = "系统用户对象", name = "sysuser", required = true)
            @Validated @RequestBody SysUser sysuser, BindingResult bindingResult
    ) {

        System.out.println(sysuser);
        if(bindingResult.hasErrors()){
            List<ObjectError> errors = bindingResult.getAllErrors();
            for (ObjectError error : errors) {
                System.out.println("errrrrrrrrrrrr");
                System.out.println(error.getDefaultMessage());
            }
            return R.error().message(bindingResult.getAllErrors().toString());
        }

        return R.ok().data("token", "admin");
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("info")
    public R loinInfo(
            @ApiParam(name = "token", value = "登录令牌", required = true)
            @RequestParam String token
    ) {
        return R.ok()
                .data("roles", "admin")
                .data("name", "admin")
                .data("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");

    }

    @PostMapping("logout")
    @ApiOperation(value = "用户登出")
    public R logout() {
        return R.ok();
    }

}
